/**
 * This file is part of OntoCompoFramework
 *
 * Copyright (C) 2011 -  Christian Brel
 *
 * @author   Main    Christian Brel
 **/
package ontocompo.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ontocompo.ui.appmanagement.Application;
import ontocompo.ui.appmanagement.ApplicationManager;
import ontocompo.ui.config.ConfigManager;
import ontocompo.ui.global.EventListenerArray;
import ontocompo.ui.global.Resources;
import ontocompo.ui.merge.PlugPane;
import ontocompo.ui.merge.PlugSelector;
import ontocompo.ui.positioning.PositioningPane;
import ontocompo.ui.projectmanagement.ProjectManager;
import ontocompo.ui.screenmanagement.Screen;
import ontocompo.ui.screenmanagement.ScreenFlowPane;
import ontocompo.ui.screenmanagement.ScreenFlowSelector;
import ontocompo.ui.screenmanagement.ScreenManager;
import ontocompo.ui.selection.SelectionTools;
import ontocompo.ui.selection.Selector;

public class MainFrame extends JFrame implements Observer {

	private ProjectManager projectManager;
	private ConfigManager configManager;
	private ApplicationManager appManager;
	private ScreenManager screenManager;

	private Selector selector;
	
	private ScreenFlowSelector screenFlowSelector;
	
	private PlugSelector plugSelector;

	private JTabbedPane contentPane;
	private JDesktopPane jDesktopPanel;
	private JPanel loadedPane; //Tab Pane 1
	private JPanel selectPane; //Tab Pane 2
	private JSplitPane splitPane ;
	private int splitPos = 0;
//	private JPanel plugPane; //Tab Pane 3
	private JPanel positioningPane; //Tab Pane 4
//	private JPanel screenflowPane; //Tab Pane 5
	private JPanel resultPane; //Tab Pane 6
	
	private final int loadedPaneIndex = 0;
	private final int selectPaneIndex = 1;
//	private final int plugPaneIndex = 2;
	//private final int positioningPaneIndex = 3;
	private final int positioningPaneIndex = 2;
//	private final int screenflowPaneIndex = 4;
	//private final int resultPaneIndex = 5;
	private final int resultPaneIndex = 3;

	private int selectedPane;


	private SelectionTools selectionTools;
	private JComponent selectionToolsFrame;
	
	private HashMap<Component, ArrayList<EventListenerArray>> componentsListeners;

	public MainFrame() {
		super();

		this.selector = new Selector();
		
		this.screenFlowSelector = new ScreenFlowSelector();
		
		this.plugSelector = new PlugSelector();
		
		
		this.configManager = new ConfigManager();
		this.configManager.loadConfiguration();

		this.appManager = new ApplicationManager();
		this.appManager.setAvailableApplications(Resources.ONTOCOMPOAPPLICATIONS);
		this.appManager.addObserver(this);
		this.appManager.addObserver(this.selector);

		this.screenManager = new ScreenManager();
		this.screenManager.addObserver(this);
		//this.screenManager.addScreen(new Screen("Default"));

		this.projectManager = new ProjectManager(this.appManager, this.screenManager);
		
		this.init();

		this.selectionTools = new SelectionTools();
		this.selectionTools.addObserver(this.selector);
		this.selectionTools.addObserver(this.screenManager);

		this.selectionToolsFrame = this.selectionTools.getToolBarFrame();

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setPreferredSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
		this.setMinimumSize(new Dimension(1024, 768));
		this.pack();
		this.setVisible(true);
	}

	public void init() {
		this.initFrameMenu();

		this.contentPane = new JTabbedPane();
		this.setContentPane(contentPane);

		this.contentPane.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				paneChanged();
			}

		});

		this.buildLoadPane();
		this.buildSelectPane();
//		this.buildPlugPane();
		this.buildPositionningPane();
//		this.buildScreenFlowPane();
		this.buildResultPane();

		this.selectedPane = loadedPaneIndex;

	}

/*	private void paneChanged() {
		int currentPane = this.contentPane.getSelectedIndex();

		if(currentPane != this.selectedPane) {
			switch(this.selectedPane) {
			case 0:
				switch(currentPane) {
				case 1:
					switchLoadPaneToSelectPane();
					splitPos = splitPane.getDividerLocation();
					break;
				case 2:
					switchToPositionningPane();
					break;
				case 3:
					switchToResultPane();
					break;
				default: ;
				}
				break;
			case 1:
				switch(currentPane) {
				case 0:
					switchSelectPaneToLoadPane();
					break;
				case 2:
					switchToPositionningPane();
					break;
				case 3:
					switchToResultPane();
					break;
				default: ;
				}
				break;
			case 2:
				switch(currentPane) {
				case 0:
					switchPositionningPaneToLoadPane();
					break;
				case 1:
					switchPositionningPaneToSelectPane();
					splitPos = splitPane.getDividerLocation();
					break;
				case 3:
					switchToResultPane();
					break;
				default: ;
				}
				break;
			case 3:
				if (currentPane == 1) splitPos = splitPane.getDividerLocation();

				switchToResultPane();
				break;
			default: ;
			}
		}
	}
	*/
	
	private void paneChanged() {
		int currentPane = this.contentPane.getSelectedIndex();
		
		if(currentPane != this.selectedPane) {
			
//			if(this.selectedPane == plugPaneIndex) {
//				this.cancelPlugPane();
//			}
//			
//			if(this.selectedPane == screenflowPaneIndex) {
//				this.cancelScreenFlowPane();
//			}
			
			if(this.selectedPane == resultPaneIndex) {
				this.cancelResultPane();
			}
			
			switch(currentPane) {
				case loadedPaneIndex:
					this.switchToLoadPane();
					break;
				case selectPaneIndex:
					this.switchToSelectPane();
					break;
//				case plugPaneIndex:
//					this.switchToPlugPane();
//					break;
				case positioningPaneIndex:
					this.switchToPositionningPane();
					break;
//				case screenflowPaneIndex:
//					this.switchToScreenFlowPane();
//					break;	
				case resultPaneIndex:
					if (currentPane == selectPaneIndex) splitPos = splitPane.getDividerLocation();
	
					this.switchToResultPane();
					break;
				default: ;
			}
		}
	}
	

	private void buildLoadPane() {
		/* TabPane 1: Loading of applications */
		this.loadedPane = new JPanel();
		contentPane.addTab("Loaded Applications", loadedPane);

		loadedPane.setLayout(new BoxLayout(loadedPane, BoxLayout.Y_AXIS));

		this.jDesktopPanel = new JDesktopPane();

		loadedPane.add(this.jDesktopPanel);
	}

	private void buildSelectPane() {
		/* TabPane 2: Selection of UI elements in order to build the new UI */
		this.selectPane = new JPanel();
		selectPane.setLayout(new BoxLayout(selectPane, BoxLayout.Y_AXIS));

		contentPane.addTab("Selection Area", selectPane);


	}
	
//	private void buildPlugPane() {
//		/* TabPane 3: Plug UI elements between them */
//		this.plugPane = new JPanel();
//		plugPane.setLayout(new BoxLayout(plugPane, BoxLayout.Y_AXIS));
//
//		contentPane.addTab("Plugging Area", plugPane);
//	}

	private void buildPositionningPane() {
		/* TabPane 4: Positionning UI elements in the different screens */
		this.positioningPane = new JPanel();
		positioningPane.setLayout(new BoxLayout(positioningPane, BoxLayout.X_AXIS));

		contentPane.addTab("Positionning Area", positioningPane);
	}
	
//	private void buildScreenFlowPane() {
//		/* TabPane 5: Describing screen flow between the different screens */
//		this.screenflowPane = new JPanel();
//		screenflowPane.setLayout(new BoxLayout(screenflowPane, BoxLayout.X_AXIS));
//
//		contentPane.addTab("Screenflow Area", screenflowPane);
//	}

	private void buildResultPane() {
		/* TabPane 6: Result application after selection */
		this.resultPane = new JPanel();
		resultPane.setLayout(new BoxLayout(resultPane, BoxLayout.Y_AXIS));

		contentPane.addTab("Result Area", resultPane);
	}

	/*private void switchLoadPaneToSelectPane() {
		this.loadedPane.remove(this.jDesktopPanel);



	 *
		this.selectPane.removeAll();
		JButton getSelectionToolsButton = new JButton("Get Selection Tools");
		getSelectionToolsButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				selectionToolsFrame.toFront();
			}

		});

		this.selectPane.add(getSelectionToolsButton);
	 *

		this.activateSelectionOnApplications();

		// this.selectPane.add(this.jDesktopPanel);

		if (splitPane == null)
			{
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
					this.selectionToolsFrame, this.jDesktopPanel);
			splitPane.setOneTouchExpandable(true);
			// splitPane.setContinuousLayout(true);
			splitPane.setDividerLocation(0);
			this.selectPane.add(splitPane);

			splitPane.addMouseMotionListener(new MouseAdapter() {
				@Override
				public void mouseMoved(MouseEvent e)
				{
						if ((e.getX() >= 0) && (e.getX()<20))
						{
	 *double d = ((double) MainFrame.this.selectionToolsFrame.getPreferredSize().getWidth())/ ((double) splitPane.getWidth());*
							int d = MainFrame.this.selectionToolsFrame.getPreferredSize().width;
							if (splitPane.getDividerLocation() < d)
							{
								splitPane.setDividerLocation(d);
								splitPane.validate();
							}
						}
				}
			});

			}
		else
		{
			this.splitPane.setRightComponent(this.jDesktopPanel);
			splitPane.setDividerLocation(splitPos);
			splitPane.validate();
		}

		this.selectedPane = 1;
		// selectPane.grabFocus();
		this.selectionTools.setToolsEnable(true);
	}*/

	/*private void switchSelectPaneToLoadPane() {
		this.selectPane.remove(this.jDesktopPanel);

		this.loadedPane.add(this.jDesktopPanel);

		this.deactivateSelectionOnApplications();

		this.selectedPane = 0;

		this.selectionTools.setToolsEnable(false);
	}*/

	private void switchToLoadPane() {
		//		this.selectPane.remove(this.jDesktopPanel);

		this.loadedPane.add(this.jDesktopPanel);

		this.deactivateSelectionOnApplications();

		this.selectedPane = loadedPaneIndex;

		this.selectionTools.setToolsEnable(false);
		
	}

	private void switchToSelectPane() {
		//		this.loadedPane.remove(this.jDesktopPanel);



		/*
		this.selectPane.removeAll();
		JButton getSelectionToolsButton = new JButton("Get Selection Tools");
		getSelectionToolsButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				selectionToolsFrame.toFront();
			}

		});

		this.selectPane.add(getSelectionToolsButton);
		 */

		this.activateSelectionOnApplications();

		// this.selectPane.add(this.jDesktopPanel);

		if (splitPane == null)
		{
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
					this.selectionToolsFrame, this.jDesktopPanel);
			splitPane.setOneTouchExpandable(true);
			// splitPane.setContinuousLayout(true);
			splitPane.setDividerLocation(0);
			this.selectPane.add(splitPane);

			splitPane.addMouseMotionListener(new MouseAdapter() {
				@Override
				public void mouseMoved(MouseEvent e)
				{
					if ((e.getX() >= 0) && (e.getX()<20))
					{
						int d = MainFrame.this.selectionToolsFrame.getPreferredSize().width;
						if (splitPane.getDividerLocation() < d)
						{
							splitPane.setDividerLocation(d);
							splitPane.validate();
						}
					}
				}
			});

		}
		else
		{
			this.splitPane.setRightComponent(this.jDesktopPanel);
			splitPane.setDividerLocation(splitPos);
			splitPane.validate();
		}

		this.selectedPane = selectPaneIndex;

		this.selectionTools.setToolsEnable(true);
		
		
	}
	
//	private void switchToPlugPane() {
//		this.plugPane.removeAll();
//		
//		this.componentsListeners = new HashMap<Component, ArrayList<EventListenerArray>>();
//		
//		ArrayList<Screen> screens = this.screenManager.getAvailabledScreens();
//		
//		for(Screen s: screens) {
//			
//			if (! s.getInBulkComponents().isEmpty())
//			{	
//				this.addPlugListener(s.getInBulkComponents());
//			}
//		}
//		
//		this.plugPane.add(new PlugPane(screens, this.plugSelector, this));
//		
//		this.selectedPane = plugPaneIndex;
//	}

	private void switchToPositionningPane() {
		this.positioningPane.removeAll();

		this.positioningPane.add(new PositioningPane(this.screenManager.getAvailabledScreens()));

		this.selectedPane = positioningPaneIndex;
		
		
	}
	
//	private void switchToScreenFlowPane() {
//		this.screenflowPane.removeAll();
//		
//		this.componentsListeners = new HashMap<Component, ArrayList<EventListenerArray>>();
//		
//		ArrayList<Screen> screens = this.screenManager.getAvailabledScreens();
//		
//		for(Screen s: screens) {
//			
//			if (s.getRootScreen() != null)
//			{
//				s.getRootScreen().drying(true);
//				s.getRootScreen().validate();
//				
//				this.addScreenFlowListener(s.getRootScreen());
//			}
//		}
//		
//		this.screenflowPane.add(new ScreenFlowPane(screens, this.screenFlowSelector));
//		
//		this.selectedPane = screenflowPaneIndex;
//		
//		
//	}
	
	private void addScreenFlowListener(java.awt.Component jc) {
		if (jc instanceof Container)
		{
			Container cont = (Container) jc;
			for(java.awt.Component subc : cont.getComponents())
			{
				addScreenFlowListener(subc);
			}
		}
		
		if(jc instanceof JButton) {
			boolean validComponent = false;
			
			for(Application app: Resources.LOADEDAPPLICATIONS) {
				if(app.isAnIdentifiedComponent(jc)) {
					validComponent = true;
				}
			}
			
			if(validComponent) {
				this.delAndStockEventListeners(jc);
				jc.addMouseListener(this.screenFlowSelector);
			}
		}
	}
	
	private void addPlugListener(ArrayList<Component> jcList) {
		for(Component jc: jcList) {
			this.addPlugListener(jc);
		}
	}
	
	public void addPlugListener(Component jc){
		if (jc instanceof Container)
		{
			Container cont = (Container) jc;
			for(java.awt.Component subc : cont.getComponents())
			{
				addPlugListener(subc);
			}
		}
		
		this.delAndStockEventListeners(jc);
		jc.addMouseListener(this.plugSelector);
	}
	
	private void delAndStockEventListeners(Component jc) {

		try {
			Method[] allMethodes = jc.getClass().getMethods();
			Set<Class<? extends EventListener>> collection = new HashSet<Class<? extends EventListener>>();
			for (Method m : allMethodes)
			{
				String name = m.getName();
				if (name.startsWith("add") && name.endsWith("Listener"))
				{
					if (m.getParameterTypes().length > 0)
					{
						Class paramType = m.getParameterTypes()[0];
						if (EventListener.class.isAssignableFrom(paramType))
						{
							collection.add(paramType);
						}
					}
				}
			}
			
			// collection.add(ActionListener.class);
			
			for(Class<? extends EventListener> c : collection)
			{
				//System.out.println(c.getSimpleName());
				EventListener[] listeners = jc.getListeners(c);
				if ((listeners == null) || (listeners.length == 0)) continue;
				
				EventListener l1 = listeners[0];
				
				String nameOfMethodToRemove = "remove"+c.getSimpleName();
				Method m;
					m = jc.getClass().getMethod(nameOfMethodToRemove, c);
				
				
				
				//Concentrator concentrator = new Concentrator (l1);
				for(EventListener l : listeners) {
					//System.out.println("debut " + l);
					m.invoke(jc, l) ;
					//System.out.println("fin " + l);
					//concentrator.addListener(l);
				}
				
				if(! this.componentsListeners.containsKey(jc)) {
					this.componentsListeners.put(jc, new ArrayList<EventListenerArray>());
				}
				ArrayList<EventListenerArray> ela = this.componentsListeners.get(jc);
				ela.add(new EventListenerArray(c, listeners));
				
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void removeScreenFlowListener(java.awt.Component c) {
		if(Application.linkedToMouseListener(c, this.screenFlowSelector)) {
			c.removeMouseListener(this.screenFlowSelector);
			this.addingStockedEventListener(c);
		}

		if (c instanceof Container)
		{
			Container cont = (Container) c;
			for(java.awt.Component subc : cont.getComponents())
			{
				removeScreenFlowListener(subc);
			}
		}
	}
	
	private void removePlugSelector(ArrayList<Component> jcList) {
		for(Component jc : jcList) {
			this.removePlugSelector(jc);
		}
	}
	
	public void removePlugSelector(java.awt.Component jc) {
		if(Application.linkedToMouseListener(jc, this.plugSelector)) {
			jc.removeMouseListener(this.plugSelector);
			this.addingStockedEventListener(jc);
		}

		if (jc instanceof Container)
		{
			Container cont = (Container) jc;
			for(java.awt.Component subc : cont.getComponents())
			{
				removePlugSelector(subc);
			}
		}
	}
	
	private void addingStockedEventListener(Component jc) {
		try {
			if(this.componentsListeners.containsKey(jc)) {
				ArrayList<EventListenerArray> eventListenerArrays = this.componentsListeners.get(jc);
				
				for(EventListenerArray ela : eventListenerArrays) {
				
					EventListener[] listeners = ela.getEvents();
					
					
					
					String nameOfMethodToAdd = "add"+ela.getEventListenerClass().getSimpleName();
					Method m;
						
					m = jc.getClass().getMethod(nameOfMethodToAdd, ela.getEventListenerClass());
					
					//Concentrator concentrator = new Concentrator (l1);
					for(EventListener l : listeners) {
						//System.out.println("debut " + l);
						m.invoke(jc, l) ;
						//System.out.println("fin " + l);
						//concentrator.addListener(l);
					}
					
					this.componentsListeners.remove(jc);
				}
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

/*	private void switchPositionningPaneToLoadPane() {
		boolean isOk = false;
		for(Component c: this.loadedPane.getComponents()) {
			if(c.equals(this.jDesktopPanel)) {
				isOk = true;
			}
		}

		if(isOk) {
			this.selectedPane = 0;
		} else {
			switchLoadPaneToSelectPane();
		}
	}
*/
	
/*	private void switchPositionningPaneToSelectPane() {
		boolean isOk = false;
		for(Component c: this.selectPane.getComponents()) {
			if(c.equals(this.jDesktopPanel)) {
				isOk = true;
			}
		}

		if(isOk) {
			this.selectedPane = 1;
		} else {
			switchLoadPaneToSelectPane();
		}
	}
*/
	private void switchToResultPane() {
		//		System.out.println("Switch to Result Pane");
		this.resultPane.removeAll();
		
		JPanel exportPanel = new JPanel();
		exportPanel.setLayout(new BoxLayout(exportPanel, BoxLayout.X_AXIS));
		
		JLabel exportAppNameLabel = new JLabel("New Application Name");
		exportPanel.add(exportAppNameLabel);
		JTextField exportAppName = new JTextField();
		exportAppName.setEnabled(false);
		exportPanel.add(exportAppName);
		JButton exportButton = new JButton("Export Application");
		exportButton.setEnabled(false);
		
		Dimension exportPanelDimension = new Dimension(this.resultPane.getWidth(), 100);
		exportPanel.setMaximumSize(exportPanelDimension);
		exportPanel.add(exportButton);
		
		this.resultPane.add(exportPanel);
		
		
		JDesktopPane jd = new JDesktopPane();

		ArrayList<Screen> screens = this.screenManager.getAvailabledScreens();

		for(Screen s: screens) {
			JInternalFrame internalframe = new JInternalFrame(s.getName(), true, true, true, true);
			if (s.getRootScreen() != null)
			{
				s.getRootScreen().drying(true);
				s.getRootScreen().validate();
				internalframe.setContentPane(s.getRootScreen());
			}


			internalframe.pack();
			internalframe.setVisible(true);
			jd.add(internalframe);
		}
		this.resultPane.add(jd);

		this.selectedPane = resultPaneIndex;
		
		
	}
	
	private void cancelPlugPane() {
		ArrayList<Screen> screens = this.screenManager.getAvailabledScreens();
		
		for(Screen s: screens) {
			
			if (! s.getInBulkComponents().isEmpty())
			{
				this.removePlugSelector(s.getInBulkComponents());
			}
		}
		
		this.cancelResultPane();
	}
	
	private void cancelScreenFlowPane() {
		ArrayList<Screen> screens = this.screenManager.getAvailabledScreens();
		
		for(Screen s: screens) {
			
			if (s.getRootScreen() != null)
			{
				s.getRootScreen().drying(false);
				s.getRootScreen().validate();
				
				this.removeScreenFlowListener(s.getRootScreen());
			}
		}
		
		this.cancelResultPane();
	}
	
	private void cancelResultPane() {
		ArrayList<Screen> screens = this.screenManager.getAvailabledScreens();

		for(Screen s: screens) {
			if (s.getRootScreen() != null)
			{
				s.getRootScreen().drying(false);
				s.getRootScreen().validate();
			}
		}
	}

	private void initFrameMenu() {
		JMenuBar frameMenu = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		fileMenu.getAccessibleContext().setAccessibleDescription(
		"File");

		JMenuItem loadAppMenuItem = new JMenuItem("Load An Application",
				KeyEvent.VK_L);
		loadAppMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_L, ActionEvent.CTRL_MASK));
		loadAppMenuItem.getAccessibleContext().setAccessibleDescription("Load An Application");
		loadAppMenuItem.addActionListener(this.appManager);
		fileMenu.add(loadAppMenuItem);

		JMenuItem saveProjectMenuItem = new JMenuItem("Save Current Project",
				KeyEvent.VK_S);
		saveProjectMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		saveProjectMenuItem.getAccessibleContext().setAccessibleDescription("Save Current Project");
		saveProjectMenuItem.setName("saveproject");
		saveProjectMenuItem.addActionListener(this.projectManager);
		fileMenu.add(saveProjectMenuItem);
		
		JMenuItem openProjectMenuItem = new JMenuItem("Open An OntoCompo Project",
				KeyEvent.VK_O);
		openProjectMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		openProjectMenuItem.getAccessibleContext().setAccessibleDescription("Open An OntoCompo Project");
		openProjectMenuItem.setName("openproject");
		openProjectMenuItem.addActionListener(this.projectManager);
		fileMenu.add(openProjectMenuItem);

		JMenuItem configMenuItem = new JMenuItem("Configuration",
				KeyEvent.VK_C);
		configMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		configMenuItem.getAccessibleContext().setAccessibleDescription("Configuration");
		configMenuItem.addActionListener(this.configManager);
		fileMenu.add(configMenuItem);


		frameMenu.add(fileMenu);

		this.setJMenuBar(frameMenu);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		if(arg0.getClass().equals(ApplicationManager.class)) {
			Application currentApp = (Application) arg1;
			JInternalFrame jInternalFrame = currentApp.getInternalFrame();

			Boolean exist = false;

			for(JInternalFrame jif : this.jDesktopPanel.getAllFrames()) {
				if(jif.equals(jInternalFrame)) {
					exist = true;
				}
			}

			if(!exist) {
				this.jDesktopPanel.add(jInternalFrame);
				jInternalFrame.toFront();
			}

		}

		if(arg0.getClass().equals(ScreenManager.class)) {
			this.selectionTools.updateScreensList(this.screenManager.getAvailabledScreens());
		}
	}

	private void activateSelectionOnApplications() {
		//		ArrayList<Application> loadedApplications = this.appManager.getLoadedApplications();

		for(Application app: Resources.LOADEDAPPLICATIONS) {
			app.activateSelection(this.selector);
		}

	}

	private void deactivateSelectionOnApplications() {
		//		ArrayList<Application> loadedApplications = this.appManager.getLoadedApplications();
		for(Application app: Resources.LOADEDAPPLICATIONS) {
			app.deactivateSelection(this.selector);
		}

	}
}
